package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.enums.ScoreChangeEnum;
import com.yupi.springbootinit.service.UserScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 积分接口
 */
@RestController
@RequestMapping("/score")
@Slf4j
public class ScoreController {

    @Resource
    private UserScoreService userScoreService;

    @GetMapping("/get")
    public BaseResponse<String> getUserScoreAll(HttpServletRequest request){
        User loginUser = userScoreService.getScoreByUser(request);
        Integer userScore = loginUser.getUserScore();
        return ResultUtils.success(userScore.toString());
    }

    @GetMapping("/signed")
    public BaseResponse<Boolean> isSigned(HttpServletRequest request) throws ParseException {
        User loginUser = userScoreService.getScoreByUser(request);
        Date signInTime = loginUser.getSignInTime();

        // 与数据库上次的签到时间比较，判断今日是否已签
        boolean b = userScoreService.compareTime(signInTime);
        if (!b){
            return ResultUtils.success(true);
        }
        return ResultUtils.success(false);
    }

    /**
     * 用户签到，先根据上次签到时间判断今日是否已签，和用户的积分数是否达上限100
     * @param request
     * @return
     */
    @PostMapping("/checkIn")
    public BaseResponse<String> getUserSignIn(HttpServletRequest request) throws ParseException {
        User loginUser = userScoreService.getScoreByUser(request);
        Integer userScore = loginUser.getUserScore();
        Date signInTime = loginUser.getSignInTime();
        // 与数据库上次的签到时间比较，判断今日是否已签
        boolean b = userScoreService.compareTime(signInTime);
        if (!b){
            return ResultUtils.error(3,"今日已签");
        }
        // 判断用户的积分数是否达上限100
        if (userScore > (ScoreChangeEnum.UP_POINTS.getValue() - ScoreChangeEnum.DAILY_LOGIN_ADD.getValue())) {
            if (userScore >= ScoreChangeEnum.UP_POINTS.getValue()) {
                return ResultUtils.error(2, "积分达上限");
            } else {
                // 积分为上限值
                userScore = ScoreChangeEnum.UP_POINTS.getValue();
            }
        }
        // 增加积分
        userScore += ScoreChangeEnum.DAILY_LOGIN_ADD.getValue();

        // 写入当前时间
        Date date = new Date();
        String nowForm = String.format("%tF", date);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = df.parse(nowForm);
        // 插入到数据库
        User user = new User();
        user.setId(loginUser.getId());
        user.setSignInTime(nowTime);
        // 存入用户签到后的积分
        user.setUserScore(userScore);
        boolean saveUserSignIn = userScoreService.updateById(user);
        if (!saveUserSignIn){
            return ResultUtils.error(4,"积分更新失败");
        }
        return ResultUtils.success("签到成功");
    }
}
